#!/bin/bash

# 配置默认参数
devices="1"  # 默认使用的设备
operator="poc"              # 默认运行poc版本用例
echo ">>>>>>>>>>>>>>>>>>> devices = $devices"

# 解析命令行参数
while getopts "d:r:" opt; do
    case $opt in
        d) devices="$OPTARG"
           echo "指定使用设备: $devices"
           ;;
        r) operator="$OPTARG"
           echo "指定运行算子用例: $operator"
           ;;
    esac
done

# 检查并设置ASCEND环境变量
if [ -n "$ASCEND_HOME_PATH" ]; then
    set_env_path="${ASCEND_HOME_PATH%/latest}/set_env.sh"
    echo "ASCEND_HOME_PATH已使能, 正在执行: source $set_env_path"
    source "$set_env_path" || { echo "source $set_env_path 失败"; exit 1; }
else
    default_path="/data/daily/ascend-toolkit/set_env.sh"
    echo "ASCEND_HOME_PATH未使能, 正在执行: source $default_path"
    source "$default_path" || { echo "source $default_path 失败"; exit 1; }
fi

# 执行用例
if [ "$operator" = "custom" ]; then
    echo "custom算子用例"
    export LD_LIBRARY_PATH=${ASCEND_HOME_PATH}/opp/vendors/customize/op_api/lib/:${LD_LIBRARY_PATH}
    # GroupedMatmul
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/GroupedMatmul_no_quant/GroupedMatmul_no_quant.json --task accuracy -p $(pwd)/atk_case/GroupedMatmul_no_quant/GroupedMatmul_no_quant.py
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/GroupedMatmul_A4W4/GroupedMatmul_A4W4.json --task accuracy -p $(pwd)/atk_case/GroupedMatmul_A4W4/GroupedMatmul_A4W4.py
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/GroupedMatmul_A8W4/GroupedMatmul_A8W4.json --task accuracy -p $(pwd)/atk_case/GroupedMatmul_A8W4/GroupedMatmul_A8W4.py
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/GroupedMatmul_A8W8O16/GroupedMatmul_A8W8O16.json --task accuracy -p $(pwd)/atk_case/GroupedMatmul_A8W8O16/GroupedMatmul_A8W8O16.py
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/GroupedMatmul_split_k/GroupedMatmul_split_k.json --task accuracy -p $(pwd)/atk_case/GroupedMatmul_split_k/GroupedMatmul_split_k.py
    # GroupedMatmulSwigluQuant
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/GroupedMatmulSwigluQuant/GroupedMatmulSwigluQuant.json --task accuracy -p $(pwd)/atk_case/GroupedMatmulSwigluQuant/GroupedMatmulSwigluQuant.py
    # MoeInitRoutingV2
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/MoeInitRoutingV2/MoeInitRoutingV2.json --task accuracy -p $(pwd)/atk_case/MoeInitRoutingV2/MoeInitRoutingV2.py
elif [ "$operator" = "poc" ]; then
    echo "开始执行所有算子用例"
    for folder in $(pwd)/atk_case/*/; do
        one_case=$(basename "$folder")
        atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/$one_case/$one_case.json --task accuracy -p $(pwd)/atk_case/$one_case/$one_case.py
    done
else
    echo "开始运行$operator算子用例"
    atk node --backend pyaclnn --devices $devices -o $(pwd) node --backend cpu task -c $(pwd)/atk_case/$operator/$operator.json --task accuracy -p $(pwd)/atk_case/$operator/$operator.py
fi

exit 0